<template>
  <div class="map-layout-container">
    <el-container>
      <el-header height="60px" class="header">
        <!-- 头部内容保持不变 -->
        <div class="header-left">
          <h2>基于GIS的高速公路道路标志标牌管理系统设计</h2>
        </div>
        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              {{ userRole === 'admin' ? '管理员' : '访客'
              }}<el-icon class="el-icon--right"><arrow-down /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      <el-container>
        <el-aside width="200px" class="aside">
          <el-menu :default-active="activeMenu" class="el-menu-vertical" @select="handleMenuSelect">
            <el-menu-item index="map">
              <el-icon><location /></el-icon>
              <span>地图监控</span>
            </el-menu-item>
            <el-menu-item index="signManagement" v-if="userRole === 'admin'">
              <el-icon><document /></el-icon>
              <span>标牌管理</span>
            </el-menu-item>
            <el-menu-item index="roadNetwork" v-if="userRole === 'admin'">
              <el-icon><connection /></el-icon>
              <span>路网管理</span>
            </el-menu-item>
            <el-menu-item index="statistics" v-if="userRole === 'admin'">
              <el-icon><pie-chart /></el-icon>
              <span>数据统计</span>
            </el-menu-item>
            <el-menu-item index="maintenance" v-if="userRole === 'admin'">
              <el-icon><tools /></el-icon>
              <span>维修管理</span>
            </el-menu-item>
            <el-menu-item index="analysisMaintenance" v-if="userRole === 'admin'">
              <el-icon><setting /></el-icon>
              <span>分析维护</span>
            </el-menu-item>
          </el-menu>
        </el-aside>
        <el-main class="main">
          <div class="map-container">
            <router-view />
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import { useRouter, RouterView } from 'vue-router'
import { ElMessage } from 'element-plus'
import {
  ArrowDown,
  Location,
  Document,
  PieChart,
  Connection,
  Tools,
  Setting,
} from '@element-plus/icons-vue'
import { useUserStore } from '../stores/modules/user'

const router = useRouter()
const userStore = useUserStore()

// 获取用户角色
const userRole = computed(() => userStore.userRole)

// 当前激活的菜单项
const activeMenu = ref('map')

const handleMenuSelect = (index: string) => {
  activeMenu.value = index
  // 根据菜单项导航到相应的路由
  if (index === 'map') {
    router.push('/map')
  } else if (index === 'signManagement' && userRole.value === 'admin') {
    router.push('/map/sign-management')
  } else if (index === 'statistics' && userRole.value === 'admin') {
    router.push('/map/statistics')
  } else if (index === 'roadNetwork') {
    router.push('/map/road-network')
  } else if (index === 'maintenance' && userRole.value === 'admin') {
    router.push('/map/maintenance')
  } else if (index === 'analysisMaintenance' && userRole.value === 'admin') {
    router.push('/map/analysis-maintenance')
  }
}
// 处理下拉菜单命令
const handleCommand = (command: string) => {
  if (command === 'logout') {
    userStore.logout()
    ElMessage.success('已成功退出登录')
    router.push('/login')
  }
}
</script>

<style scoped>
.map-layout-container {
  height: 100vh;
  width: 100%;
}

.header {
  background-color: #1976d2;
  color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.header-left h2 {
  margin: 0;
  font-size: 20px;
}

.header-right {
  display: flex;
  align-items: center;
}

.el-dropdown-link {
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
}

.aside {
  background-color: #f5f7fa;
  border-right: 1px solid #e6e6e6;
}

.main {
  padding: 0;
  background-color: #f5f7fa;
}

.map-container {
  height: 100%;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  position: relative;
}

.map-view {
  width: 100%;
  height: 100%;
}
</style>
